/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.manage.service.impl;

import me.zhengjie.modules.manage.domain.Scenic;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.manage.repository.ScenicRepository;
import me.zhengjie.modules.manage.service.ScenicService;
import me.zhengjie.modules.manage.service.dto.ScenicDto;
import me.zhengjie.modules.manage.service.dto.ScenicQueryCriteria;
import me.zhengjie.modules.manage.service.mapstruct.ScenicMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author yyj
* @date 2024-02-26
**/
@Service
@RequiredArgsConstructor
public class ScenicServiceImpl implements ScenicService {

    private final ScenicRepository scenicRepository;
    private final ScenicMapper scenicMapper;

    @Override
    public Map<String,Object> queryAll(ScenicQueryCriteria criteria, Pageable pageable){
        Page<Scenic> page = scenicRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(scenicMapper::toDto));
    }

    @Override
    public List<ScenicDto> queryAll(ScenicQueryCriteria criteria){
        return scenicMapper.toDto(scenicRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ScenicDto findById(String id) {
        Scenic scenic = scenicRepository.findById(id).orElseGet(Scenic::new);
        ValidationUtil.isNull(scenic.getId(),"Scenic","id",id);
        return scenicMapper.toDto(scenic);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ScenicDto create(Scenic resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return scenicMapper.toDto(scenicRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Scenic resources) {
        Scenic scenic = scenicRepository.findById(resources.getId()).orElseGet(Scenic::new);
        ValidationUtil.isNull( scenic.getId(),"Scenic","id",resources.getId());
        scenic.copy(resources);
        scenicRepository.save(scenic);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            scenicRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ScenicDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ScenicDto scenic : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("景点名称", scenic.getScenicName());
            map.put("x轴", scenic.getGisx());
            map.put("y轴", scenic.getGisy());
            map.put("省", scenic.getProvince());
            map.put("市", scenic.getCity());
            map.put("县", scenic.getTown());
            map.put("攻略", scenic.getRemark());
            map.put("路线", scenic.getRoute());
            map.put("景区价格", scenic.getScenicPrice());
            map.put("图片路径", scenic.getPicPath());
            map.put("是否游玩", scenic.getIsFlag());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}